//
//  SubjectCell.swift
//  clreader
//
//  Created by colin on 2019/3/6.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

extension HomeViewController {
    
    class SubjectCell: UITableViewCell {
        
        var itemHandle:((_ subject:JSONObject?)->Void)?
        var cellHeight:CGFloat = 0
        
        static let reuseId = "SubjectCell"
        
        private var subjects:[JSONObject]?
        
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
            self.selectionStyle = .none
        }
        
        func refresh(data:[JSONObject]?) {
            
            self.subjects = data
            self.cellHeight = 0
            for subview in self.contentView.subviews {
                subview.removeFromSuperview()
            }
            
            if let subjects = data,!subjects.isEmpty {
                
                let distance:CGFloat = 5.0
                let columnCount = 3
                var x:CGFloat = 15.0
                var y:CGFloat = 0
                let width = (UIScreen.main.bounds.width - 15.0 - 15.0 - (CGFloat(columnCount) - 1) * distance) / CGFloat(columnCount)
                let height:CGFloat = 70.0
                
                // 总高度
                let rowCount = subjects.count / columnCount + (subjects.count % columnCount == 0 ? 0 : 1)
                self.cellHeight = height * CGFloat(rowCount) + distance * CGFloat(rowCount - 1)
                
                for index in 0 ..< subjects.count {
                    let subject = subjects[index]
                    
                    let item = CLTouchableView().then {
                        $0.layer.cornerRadius = 3.0
                        $0.layer.masksToBounds = true
                        $0.isUserInteractionEnabled = true
                        $0.tag = index
                        let tap = UITapGestureRecognizer(target: self, action: #selector(self.itemClickAction(_:)))
                        $0.addGestureRecognizer(tap)
                        
                        let imageViewBg = UIImageView(image: UIImage(named: "home_subject_bg"))
                        $0.addSubview(imageViewBg)
                        imageViewBg.snp.makeConstraints({ (make) in
                            make.edges.equalToSuperview()
                        })
                        
                        let imageViewTag = UIImageView(image: UIImage(named: "home_subject_tag"))
                        $0.addSubview(imageViewTag)
                        imageViewTag.snp.makeConstraints({ (make) in
                            make.size.equalTo(CGSize(width: 20.0, height: 20.0))
                            make.left.equalToSuperview().offset(5.0)
                            make.centerY.equalToSuperview()
                        })
                        
                        let labelTitle = UILabel().then {
                            $0.text = subject["title"] as? String
                            $0.textColor = UIColor.white
                            $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                            $0.numberOfLines = 2
                        }
                        $0.addSubview(labelTitle)
                        labelTitle.snp.makeConstraints({ (make) in
                            make.right.equalToSuperview().offset(-5.0)
                            make.left.equalTo(imageViewTag.snp.right).offset(5.0)
                            make.top.equalToSuperview().offset(14.0)
                        })
                        
                        let labelCount = UILabel().then {
                            if let res_count = subject["res_count"] as? Int {
                                $0.text = "\(res_count)个作品"
                            }
                            $0.textColor = UIColor(hex: "DFDFDF")
                            $0.font = UIFont.systemFont(ofSize: 10.0, weight: .light)
                        }
                        $0.addSubview(labelCount)
                        labelCount.snp.makeConstraints({ (make) in
                            make.left.equalTo(imageViewTag.snp.right).offset(5.0)
                            make.bottom.equalToSuperview().offset(-10.0)
                        })
                        
                        let imageViewArrow = UIImageView(image: UIImage(named: "icon_arrow_right"))
                        $0.addSubview(imageViewArrow)
                        imageViewArrow.snp.makeConstraints({ (make) in
                            make.size.equalTo(CGSize(width: 5.0, height: 8.0))
                            make.left.equalTo(labelCount.snp.right).offset(3.0)
                            make.centerY.equalTo(labelCount.snp.centerY)
                        })
                        
                        
                    }
                    self.contentView.addSubview(item)
                    item.snp.makeConstraints { (make) in
                        make.left.equalToSuperview().offset(x)
                        make.top.equalToSuperview().offset(y)
                        make.size.equalTo(CGSize(width: width, height: height))
                        
                        // 需要换行
                        if (index + 1) % columnCount == 0 {
                            y += height + distance
                            x = 15.0
                        } else {
                            x += width + distance
                        }
                        
                    }
                }
            }
            
        }
        
        @objc private func itemClickAction(_ sender:UIGestureRecognizer?) {
            if let index = sender?.view?.tag,
                let subjects_ = self.subjects,
                subjects_.count > index {
                self.itemHandle?(subjects_[index])
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}
